package com.lagou.task.dao;

import com.lagou.task.entity.Admin;
import com.lagou.task.utils.Dbutils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * AdminDao的实现类
 */
public class AdminDaoImpl implements AdminDao{

    @Override
    public Admin adminLogin(Admin admin) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            // 获得数据库连接池中的连接
            connection = Dbutils.getConnection();
            // 定义sql语句
            String sql = "select * from admin where name = ? and password = ?";
            // 获取PreparedStatement
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,admin.getName());
            preparedStatement.setString(2,admin.getPassword());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                Admin query = new Admin(resultSet.getString("name"),resultSet.getString("password"));
                return query;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //Dbutils.close(connection,preparedStatement,resultSet);
        }
        return null;
    }
}
